/*
 * Copyright 2013 stanislawbartkowski@gmail.com 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package com.gwtmodel.table.view.util;

import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwtmodel.table.WSize;
import com.gwtmodel.table.common.ISignal;

/**
 * 
 * @author stanislawbartkowski@gmail.com
 */
public class ClickPopUp {

    private PopupPanel pUp;

    public void setVisible(final boolean visible) {
        pUp.setVisible(visible);
    }

    private class CloseH implements CloseHandler<PopupPanel> {

        private final ISignal i;

        CloseH(ISignal i) {
            this.i = i;
        }

        public void onClose(CloseEvent<PopupPanel> event) {
            i.signal();
        }
    }

    private void setD(final WSize w, final Widget showW, ISignal i) {
        pUp = new PopupPanel(true);
        if (i != null) {
            pUp.addCloseHandler(new CloseH(i));
        }
        PopupUtil.setPos(pUp, w);
        pUp.setWidget(showW);
        pUp.show();
    }

    public ClickPopUp(final WSize w, final Widget showW) {
        setD(w, showW, null);
    }

    public ClickPopUp(final Widget w, final Widget showW) {
        setD(new WSize(w), showW, null);
    }

    public ClickPopUp(final WSize w, final Widget showW, ISignal i) {
        setD(w, showW, i);
    }
}
